/**
  ******************************************************************************
  * @file   :   .c
  * @version:
  * @author :
  * @brief  :
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) . BLUEX Microelectronics.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */
/* config --------------------------------------------------------------------*/

/* includes ------------------------------------------------------------------*/
#include "true_random.h"
#include "rc_calib.h"
#include "stdint.h"
#include "string.h"
#include "plf.h"
#include "reg_sysc_awo.h"
#include "reg_sysc_cpu.h"
#include "clk_gate.h"

/* private define ------------------------------------------------------------*/

/* private typedef -----------------------------------------------------------*/

/* private variables ---------------------------------------------------------*/
uint32_t   rand_seed;

/* exported variables --------------------------------------------------------*/

/* private macros ------------------------------------------------------------*/

/*============================= private function =============================*/
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static uint8_t get_32k_val_8bit()
{
    uint32_t cal_val = 0;
    uint8_t  retval = 0;
    //start cal
    rc_calib_start();
    rc_calib_end( true );
    cal_val = get_rc32k_calib_val();
    //return
    retval = cal_val & 0xFF;
    return retval;
}
///** ---------------------------------------------------------------------------
// * @brief   :
// * @note    :
// * @param   :
// * @retval  :
//-----------------------------------------------------------------------------*/
//static void start_32k_xtal(void)
//{
//    sysc_awo_o_ana_32k_selxtal_setf(1);
//    BX_DELAY_US(150);
//    sysc_awo_o_32k_rcosc_en_setf(0);
//}
///** ---------------------------------------------------------------------------
// * @brief   :
// * @note    :
// * @param   :
// * @retval  :
//-----------------------------------------------------------------------------*/
//static void start_32k_rc(void)
//{
//    sysc_awo_o_32k_rcosc_en_setf(1); 
//    BX_DELAY_US(60*1000);
//    sysc_awo_o_ana_32k_selxtal_setf(0); 
//    BX_DELAY_US(150);
//}
/*========================= end of private function ==========================*/


/*============================= exported function ============================*/

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
uint32_t get_random_seed( void )
{
    return rand_seed;
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void hw_trng_get_numbers( uint8_t * buffer, uint8_t length )
{
    for( uint8_t i = 0; i < length; i++ ) {
        uint8_t tmp = 0;
        tmp |= get_32k_val_8bit() & 0x0F;
        tmp <<= 4;
        tmp |= get_32k_val_8bit() & 0x0F;
        buffer[i] = tmp;
    }
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void generate_random_seed( void )
{
    hw_trng_get_numbers( ( uint8_t * )&rand_seed, sizeof( rand_seed ) );
}

/*========================= end of exported function =========================*/


/*============================= import function ==============================*/

/*=========================== end of import function =========================*/


/*============================ interrupt function ============================*/

/*========================= end of interrupt function ========================*/


/******************** (C) COPYRIGHT BLUEX **********************END OF FILE****/
